package org.example.utils;

import okhttp3.*;

import java.io.File;
import java.io.IOException;

public class ApiClient {

    private static final String URL = "YOUR-API-URL-CREATED-IN-MODELARTS";
    private static final String TOKEN = "YOUR-CLOUD-TOKEN";
    private static final String FILE_PATH = "/path/to/local/character_line_image.jpg";

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        // 创建文件对象
        File file = new File(FILE_PATH);

        // 构建请求体
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("images", file.getName(),
                        RequestBody.create(MediaType.parse("image/jpeg"), file))
                .build();
        // 构建请求
        Request request = new Request.Builder()
                .url(URL)
                .post(requestBody)
                .header("x-auth-token", TOKEN)
                .build();

        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // 假设服务器返回的是JSON格式的数据
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}